package com.ozo.boot.gen.util;

import cn.hutool.extra.spring.SpringUtil;
import com.ozo.boot.common.base.constant.CommonFieldConstant;
import com.ozo.boot.common.base.util.NamingConventionUtils;
import com.ozo.boot.gen.constant.*;
import com.ozo.boot.gen.entity.GenColumnMap;
import com.ozo.boot.gen.entity.GenProject;
import com.ozo.boot.gen.entity.GenTable;
import com.ozo.boot.gen.entity.GenTableColumn;
import com.ozo.boot.gen.pojo.bo.GenTableBO;
import com.ozo.boot.gen.pojo.bo.GenTableColumnBO;
import com.ozo.boot.gen.pojo.dto.GenColumnMapQueryDTO;
import com.ozo.boot.gen.service.IGenColumnMapService;
import com.ozo.boot.gen.service.IGenProjectService;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 代码生成工具类
 *
 * @author qiangesoft
 * @date 2024-11-12
 */
public class TableColumnInitUtil {

    /**
     * 下划线
     */
    public static final char UNDERLINE = '_';

    /**
     * 初始化表
     */
    public static GenTable initTable(GenTableBO tableBO) {
        String tableName = tableBO.getTableName();

        GenTable table = new GenTable();
        table.setTableName(tableName);
        table.setTableComment(tableBO.getTableComment());
        table.setGenModule(getFirstWord(tableName));
        table.setGenClass(NamingConventionUtils.underlineToCamel(tableName, false));
        table.setGenFunction(tableBO.getTableComment());
        table.setGenOperateLog(true);
        table.setGenAdd(true);
        table.setGenRemove(true);
        table.setGenEdit(true);
        table.setGenSelect(true);
        table.setGenPage(true);
        table.setGenPageTableType(PageTableType.PAGE.getCode());
        table.setGenPageFormType(PageFormType.ONE.getCode());
        table.setGenPageFormOpen(PageFormOpenType.DIALOG.getCode());
        table.setRemark(null);

        // 项目默认配置
        IGenProjectService projectService = SpringUtil.getBean(IGenProjectService.class);
        GenProject project = projectService.get();
        if (project != null) {
            table.setProjectName(project.getProjectName());
            table.setProjectCode(project.getProjectCode());
            table.setGenAuthor(project.getProjectAuthor());
            table.setGenAuthorEmail(project.getProjectAuthorEmail());
            table.setGenVersion(project.getProjectVersion());
            table.setGenPackage(project.getProjectPackage());
            table.setGenBackendPath(project.getProjectBackendPath());
            table.setGenFrontPath(project.getProjectFrontPath());
        }
        return table;
    }

    /**
     * 初始化表字段
     */
    public static List<GenTableColumn> initTableColumn(Long tableId, String dbType, List<GenTableColumnBO> columnBOList) {
        IGenColumnMapService columnMapService = SpringUtil.getBean(IGenColumnMapService.class);
        GenColumnMapQueryDTO queryDTO = new GenColumnMapQueryDTO();
        queryDTO.setDbType(dbType);
        Map<String, GenColumnMap> columnMapMap = columnMapService.list(queryDTO).stream().collect(Collectors.toMap(GenColumnMap::getColumnType, genColumnMap -> genColumnMap));

        List<GenTableColumn> tableColumnList = new ArrayList<>();
        for (int i = 0; i < columnBOList.size(); i++) {
            GenTableColumnBO columnBO = columnBOList.get(i);

            GenTableColumn tableColumn = new GenTableColumn();
            tableColumn.setTableId(tableId);
            // 字段信息
            tableColumn.setColumnName(columnBO.getColumnName());
            tableColumn.setColumnComment(columnBO.getColumnComment());
            tableColumn.setColumnType(columnBO.getColumnType());
            tableColumn.setColumnPk(columnBO.getColumnPk());
            tableColumn.setColumnNotNull(columnBO.getColumnNotNull());
            tableColumn.setColumnIncrement(columnBO.getColumnIncrement() != null && columnBO.getColumnIncrement());
            tableColumn.setDataLength(columnBO.getDataLength());
            tableColumn.setDecimalLength(columnBO.getDecimalLength());
            // java类型信息
            tableColumn.setJavaField(NamingConventionUtils.underlineToCamel(columnBO.getColumnName(), true));
            GenColumnMap columnMap = columnMapMap.get(columnBO.getColumnType());
            tableColumn.setPageDictType(null);
            if (columnMap != null) {
                tableColumn.setJavaType(columnMap.getJavaType());
                tableColumn.setJavaTypePackage(columnMap.getJavaTypePackage());
                tableColumn.setPageHtmlType(columnMap.getHtmlType());
            } else {
                tableColumn.setJavaType("String");
                tableColumn.setJavaTypePackage("java.lang.String");
                tableColumn.setPageHtmlType(HtmlType.INPUT.getCode());
            }
            // 页面信息
            tableColumn.setPageFormRequired(!columnBO.getColumnPk() && columnBO.getColumnNotNull());
            tableColumn.setPageForm(!columnBO.getColumnPk() && !arraysContains(CommonFieldConstant.NOT_DISPLAY_COLUMN, columnBO.getColumnName()));
            tableColumn.setPageTable(tableColumn.getPageForm());
            tableColumn.setPageTableQuery(false);
            tableColumn.setPageTableQueryType(null);
            tableColumn.setOrderNum(i + 1);

            tableColumnList.add(tableColumn);
        }
        return tableColumnList;
    }

    /**
     * 校验数组是否包含指定值
     *
     * @param array
     * @param value
     * @return
     */
    public static boolean arraysContains(String[] array, String value) {
        return Arrays.asList(array).contains(value);
    }

    /**
     * 获取第一个单词
     *
     * @param str
     * @return
     */
    public static String getFirstWord(String str) {
        int firstIndex = str.indexOf(UNDERLINE);
        if (firstIndex != -1) {
            return str.substring(0, firstIndex);
        }
        return str;
    }

}
